<template>
  <div class="assets">
    <div>
      <jc-button
        :btn-arr="['reload', 'voucher', 'save', 'sub', 'remove', 'pass', 'list']"
        :disab-arr="[isDisabled && 'save']"
        :list="'/assetsList'"
        @refresh="handleGetUntreated"
        @save="save"
        @submit="save(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="remove"
        @voucher="credentialsTask"
        @voucherLok="LooktialsTask"
      />
    </div>
    <div class="title">{{ $t('market.info') }}</div>
    <el-form :model="proValue" label-width="120px" size="mini" :rules="rules">
      <el-form-item :label="$t('shop.state')">
        <el-tag :type="proValue.stateColor">{{ proValue.fdocumentstatus }}</el-tag>
      </el-form-item>
      <el-form-item :label="$t('shop.fnumber')">
        <el-input v-model="proValue.fbillno" disabled />
      </el-form-item>
      <el-form-item :label="$t('market.satartUseDate')" prop="fdate">
        <el-date-picker
          v-model.trim="proValue.fdate"
          type="date"
          value-format="yyyy-MM-dd"
          disabled
          style="width: 152px"
        />
      </el-form-item>
      <el-form-item :label="$t('market.assetstype')" prop="fassetstypeid">
        <el-select v-model="proValue.fassetstypeid" :disabled="isDisabled">
          <el-option
            v-for="item in billtype"
            :key="item.fid"
            :label="item.fname"
            :value="item.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.assetsname')" prop="fassetsname">
        <el-input v-model="proValue.fassetsname" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item :label="$t('market.measuring')" prop="funitid">
        <el-input v-model="proValue.funitname" :disabled="isDisabled">
          <i
            slot="suffix"
            class="el-input__icon el-icon-search"
            @click="isBaseUnit=true"
          />
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('market.assetQty')" prop="fassetsamount">
        <el-input-number v-model="proValue.fassetsamount" controls-position="right" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item :label="$t('market.assetStatus')">
        <el-select v-model="proValue.fstatusid" :disabled="isDisabled">
          <el-option
            v-for="item in billtype3"
            :key="item.fid"
            :label="item.fname"
            :value="item.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.changeid')">
        <el-select v-model="proValue.fchangeid" :disabled="isDisabled">
          <el-option
            v-for="item in billtype4"
            :key="item.fid"
            :label="item.fname"
            :value="item.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.note')">
        <el-input v-model="proValue.fremark" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item :label="$t('market.fmaterial')">
        <el-input v-model="proValue.fnumber" :disabled="isDisabled">
          <i
            slot="suffix"
            class="el-input__icon el-icon-search"
            @click="isMateria=true"
          />
        </el-input>
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('market.fince')" name="first">
        <fince :fince.sync="proValue" :isabled="isDisabled" />
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="second">
        <matter :table-data.sync="matter" :del-arr.sync="delArr" :isabled="isDisabled" :num="proValue.fassetsamount" />
      </el-tab-pane>
    </el-tabs>
    <material v-if="isMateria" :fisasset="'1'" @material="materialData" />
    <baseUnit v-if="isBaseUnit" :msg="false" @unit="selBaseunit" />
  </div>
</template>

<script>
import {
  deleteAssetss,
  noreviewAssets,
  nosubmitAssets,
  queryAssetsDetails,
  reviewAssets,
  submitAssets,
  updateAssets
} from '@/api/financial/assets'
import { queryViewItem } from '@/api/marketManage/marketOrder'
import fince from '@/views/financial/assets/assetsAdd/commponent/fince'
import matter from '@/views/financial/assets/assetsAdd/commponent/matter'
import jcButton from '@/components/Button'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'
import { createVoucherByCard } from '@/api/financial/cashier'
export default {
  components: {
    jcButton,
    fince,
    matter,
    baseUnit: () => import('@/views/basic/createMateriel2/components/baseUnit'),
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material')
  },
  data() {
    return {
      activeName: 'first',
      isMateria: false, // 物料
      isBaseUnit: false,
      isDisabled: false,
      proValue: {},
      billtype: [], // 资产类别
      billtype3: [], // 资产状态
      billtype4: [], // 资产状态
      matter: [], // 其他信息
      delArr: [],
      rules: {
        fassetstypeid: [{ required: true, message: '资产类别不能为空!', trigger: 'blur' }],
        fassetsname: [{ required: true, message: '资产名称不能为空!', trigger: 'blur' }],
        funitid: [{ required: true, message: '计量单位不能为空!', trigger: 'blur' }],
        fassetsamount: [{ required: true, message: '资产数量不能为空!', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.queryTBasBilltype()
    this.queryTBasBilltype3()
    this.queryTBasBilltype4()
    if (this.$route.params.id) {
      this.handleGetUntreated()
    }
  },
  methods: {
    // 详情
    handleGetUntreated() {
      const fid = this.$route.params.id
      queryAssetsDetails({ fid }).then(res => {
        if (res.code === 0) {
          this.matter = res.data.details
          this.proValue = toMxBmina(res.data)
          this.isDisabled = this.proValue.fdocumentstatus === this.$t('market.state1') || this.proValue.fdocumentstatus === this.$t('market.state2')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 修改
    save(ev) {
      this.staEnd = false
      this.usedep = false
      this.fstocknum = 0
      this.matter.forEach(res => {
        if (res.fuseenddate < res.fusestartdate) {
          this.staEnd = true
        }
        if (!res.fusedepname) {
          this.usedep = true
        }
        this.fstocknum += res.fstockamount
      })
      if (this.fstocknum !== this.proValue.fassetsamount) {
        this.$confirm('其他信息-领用数量之和必须等于资产数量！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })

        return
      }
      if (this.staEnd) {
        this.$confirm('其他信息-结束日期不能小于开始日期,使用部门、使用人为必填项.', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        this.staEnd = false
        return
      }
      if (this.usedep) {
        this.$confirm('使用部门、使用人为必填项!!!', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        this.usedep = false
        return
      }
      const DATA = {
        ...notoMxBmina(this.proValue),
        details: this.matter,
        delArr: this.delArr
      }
      updateAssets(DATA).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.submit(1) : this.handleGetUntreated()
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 资产类别
    async queryTBasBilltype() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_ASSETS_TYPE' })
      this.billtype = RES.list
    },
    // 资产状态
    async queryTBasBilltype3() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_ASSETS_STATUS' })
      this.billtype3 = RES.list
    },
    // 变动方式
    async queryTBasBilltype4() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_ASSETS_CHANGE' })
      this.billtype4 = RES.list
    },
    // 物料编码
    materialData(item) {
      if (item.fmaterialId) {
        this.proValue.fmaterialid = item.fmaterialId
        this.proValue.fnumber = item.fnumber
      }
      this.isMateria = false
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.proValue.fbillno] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? submitAssets : nosubmitAssets
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? reviewAssets : noreviewAssets
      this.toExamineFun(api)
    },
    // 删除
    remove() {
      this.$confirm('确定要删除该订单么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const DATA = { Numbers: [this.proValue.fbillno] }
        deleteAssetss(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/assetsList' })
        })
      })
    },
    // 计量单位
    selBaseunit(ev) {
      if (ev.funitid) {
        this.proValue.funitname = ev.fname
        this.proValue.funitid = ev.funitid
      }
      this.isBaseUnit = false
    },
    // 凭证生成
    credentialsTask() {
      const DATA = {
        fids: [this.proValue.fid]
      }
      createVoucherByCard(DATA).then(res => {
        if (res.code === 0) {
          this.$message({ showClose: true, message: res.message, type: 'success' })
          this.$router.push({ path: '/certificate' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 查看凭证
    LooktialsTask() {
      const id = this.proValue.voucherid
      if (id) {
        this.$router.push({ path: `/certificateDetail/${id}` })
      } else {
        this.$confirm('该单据未能查询到对应的业务凭证！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.assets {
  @include listBom;
  .title {
    font-size: 14px;
    margin: 5px 0;
    color: #1e9fff;
    padding-bottom: 2px;
    border-bottom: 2px solid #e4e7ed;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 30px !important;
      width: 275px;
      margin-bottom: 5px;
    }
  }
}
</style>

